package com.aliengen.thingple.driverapp.ui.adapters;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.aliengen.thingple.driverapp.R;
import com.aliengen.thingple.driverapp.data.models.Anomaly;
import com.aliengen.thingple.driverapp.ui.fragments.AnomaliesListFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * This class is part of the Thingple mobile application for the CMC168 Handheld reader.
 *
 * Copyright AlienGen Limited 2016. All rights reserved.
 *
 * @author Timothé Mermet-Buffet <tim@aliengen.com>
 */
public class AnomaliesListAdapter extends RecyclerView.Adapter<AnomaliesListAdapter.ViewHolder> {
    private List<Anomaly> mValues;
    private final AnomaliesListFragment.OnListFragmentListener mListener;

    public AnomaliesListAdapter(AnomaliesListFragment.OnListFragmentListener listener) {
        mValues = new ArrayList<>();
        this.mListener = listener;
    }

    public void updateList(List<Anomaly> values)
    {
        mValues = values;
        notifyDataSetChanged();
    }

    public List<Anomaly> getList()
    {
        return mValues;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent,
                                         int viewType) {
        View itemLayoutView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.adapter_anomaly, null);

        ViewHolder viewHolder = new ViewHolder(itemLayoutView);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, int position) {
        viewHolder.mReferenceHuashen.setText(mValues.get(position).getHuashenNumber());
        viewHolder.mControlPoint.setText(mValues.get(position).getControlPoint());

    }

    @Override
    public int getItemCount() {
        return mValues.size();
    }

    public Anomaly getItem(int position) {
        return mValues.get(position);
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        public final TextView mReferenceHuashen;
        public final TextView mControlPoint;

        public ViewHolder(View view) {
            super(view);
            mReferenceHuashen = (TextView) view.findViewById(R.id.cylinder_number);
            mControlPoint = (TextView) view.findViewById(R.id.anomaly);
        }
    }

}
